﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using DAL;
using Model;

namespace GEMINIMainOP
{
    public partial class FormUserManagement : Form
    {
        public FormUserManagement()
        {
            InitializeComponent();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                UserLogin userLogin = UserManagement.GetUser(textBox1.Text);
                if (userLogin.Admin == null)
                {
                    try
                    {
                        int disnum = UserManagement.AddUser(comboBox1.Text, textBox1.Text, textBox2.Text, Convert.ToInt32(textBox4.Text), textBox5.Text);
                    


                        if (disnum == 1)
                        {
                            MessageBox.Show("用户添加成功！", "用户提示！");
                        }
                        else
                        {
                            MessageBox.Show("用户添加失败！", "用户提示");
                        }
                    }
                    catch (Exception ex1)
                    {
                        MessageBox.Show(Convert.ToString(ex1), "错误提示!");
                    }

                }
                else
                {
                    MessageBox.Show("用户名已存在，请重新输入！", "用户提示");
                }

            }
            else
            {
                MessageBox.Show("用户名或密码为空，请重新输入！", "用户提示");
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            int disnum = UserManagement.DeleteUser(textBox3.Text);
            if (disnum == 1)
            {
                MessageBox.Show("用户删除成功！", "用户提示");
            }
            else
            {
                MessageBox.Show("用户删除失败！", "用户提示");
            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            #region//查询所有用户
            /// <summary>
            /// 查询所有用户
            /// </summary>
            /// <param name="admin"></param>
            /// <param name="pwd"></param>
            /// <returns></returns>
            String sql = "select userID,admin,serialNumber,name from [User] ";

            List<UserLogin> objUsers = new List<UserLogin>();
            try
            {
                SqlDataReader objreader = SQLHelp.GetReader(sql);
                while (objreader.Read())
                {
                    UserLogin userLogin = new UserLogin();
                    userLogin.Admin = objreader["admin"].ToString();
                    userLogin.UserID = Convert.ToInt32(objreader["userID"].ToString());
                    userLogin.serialNumber = Convert.ToInt32(objreader["serialNumber"].ToString());
                    userLogin.name = objreader["name"].ToString();
                    objUsers.Add(userLogin);
                }
                this.dataGridView1.AutoGenerateColumns = false;
                this.dataGridView1.DataSource = objUsers;
                objreader.Close();
            }
            catch (Exception ex)
            {
                //将错误打印出来
                throw new Exception("数据访问发生异常！" + ex.Message);
            }

            #endregion
        }

        private void FormUserManagement_Load_1(object sender, EventArgs e)
        {
            comboBox1.Items.Add("1");
            comboBox1.Items.Add("2");
            comboBox1.Items.Add("3");
            
            comboBox2.Items.Add("1");
            comboBox2.Items.Add("2");
            comboBox2.Items.Add("3");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {

                int anum = UserManagement.UpdateUser(textBox9.Text, textBox8.Text, comboBox2.Text, Convert.ToInt32(textBox7.Text), textBox6.Text);
                if (anum == 1)
                {
                    MessageBox.Show("修改成功！");
                }

            }
            catch(Exception ex1)
            {
                MessageBox.Show(Convert.ToString(ex1), "错误提示!");
            }

        }
    }
}
